/**
 * @author ChaP
 * @date 2019/05/09
 * @describe:
 */
package CodingTest.AC20190509;

import java.util.concurrent.ConcurrentHashMap;

public class countBattleships {
    public int countBattleships(char[][] board) {
        int res = 0,height = board.length,width = board[0].length,i,j,m,n;
        boolean[][] visited = new boolean[height][width];
        for(i=0;i<height;i++)
            for(j=0;j<width;j++)
                visited[i][j] = false;
        for(i = 0;i<height;i++)
            for(j=0;j<width;j++)
                if(board[i][j]=='x' && visited[i][j]==false){
                    res++;
                    DFS(board,visited,i,j,height,width);
                }
                return res;
    }
    public void DFS(char[][] board,boolean[][] visited,int i,int j,int h,int w){
        int ii,jj,k;
        visited[i][j] = true;
        //向上
        ii=i-1;jj=j;
        if(ii>0 && ii<h && jj>0 && jj<w && board[ii][jj]=='x' && visited[ii][jj] == false)
            DFS(board,visited,ii,jj,h,w);
        //向下
        ii=i+1;jj=j;
        if(ii>0 && ii<h && jj>0 && jj<w && board[ii][jj]=='x' && visited[ii][jj] == false)
            DFS(board,visited,ii,jj,h,w);
        //向左
        ii=i;jj=j-1;
        if(ii>0 && ii<h && jj>0 && jj<w && board[ii][jj]=='x' && visited[ii][jj] == false)
            DFS(board,visited,ii,jj,h,w);
        //向右
        ii=i;jj=j+1;
        if(ii>0 && ii<h && jj>0 && jj<w && board[ii][jj]=='x' && visited[ii][jj] == false)
            DFS(board,visited,ii,jj,h,w);
    }

    //dfs


}
